#include <stdio.h>
#include <stdlib.h>

struct linked_list_t
{
    int num;
    struct linked_list_t *previous;
    struct linked_list_t *next;
};

struct linked_list_t *base;

int main(int argc, char *argv[])
{
    int n;
    scanf("%d", &n);

    base = (struct linked_list_t *)malloc(sizeof(struct linked_list_t) * n);
    base->previous = base + n - 1;
    (base + n - 1)->next = base;
    for (int i = 0; i < n - 1; i++)
    {
        base[i].next = base + i + 1;
        base[i + 1].previous = base + i;
    }

    for (int i = 0; i < n; i++)
    {
        scanf("%d", &base[i].num);
    }

    struct linked_list_t *ptr = base;
    struct linked_list_t *checkptr = base;
    int power = 0;
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i; j++)
        {
            checkptr = checkptr->next;
            if (ptr->num > checkptr->num)
            {
                ptr = checkptr;
            }
        }
        power += ptr->num * ptr->next->num * ptr->previous->num;

        ptr->next->previous = ptr->previous;
        ptr->previous->next = ptr->next;
        checkptr = ptr = ptr->next;
    }

    printf("%d\n",power);
    free(base);
    return 0;
}
